// pages/phone/index.js
const rules = require('../../utils/util.js')
const service = require("../../service/phone")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    phoneErrorText: "",
    Length: 6, //输入框个数
    isFocus: true, //聚焦
    Value: "", //输入的内容
    ispassword: false, //是否密文显示 true为密文， false为明文。
    timeCountDownTop: "获取验证码",
    second: 60,
    counting: false,
    code: "",
    show: false
  },
  blurEventphone(e) {
    if (e.detail.value == '') {
      this.setData({
        phoneErrorText: '电话号码不能为空！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
        phone: e.detail.value
      });
    }
  },
  onChangephone(e) {
    //console.log(e.detail)
    if (!rules.regPhone(e.detail)) {
      this.setData({
        phoneErrorText: '手机号格式有误！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
      });
    }
  },
  next() {
    if (this.data.phone == '') {
      this.setData({
        phoneErrorText: '电话号码不能为空！',
      });

      return
    }
    if (!rules.regPhone(this.data.phone)) {
      this.setData({
        phoneErrorText: '手机号格式有误！',
      });

      return
    }
    wx.navigateTo({
      url: '/pages/phone/verifi/index?phone=' + this.data.phone,
    })
  },
  Focus(e) {
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value;
    that.setData({
      Value: inputValue,
    })
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  },
  formSubmit(e) {
    console.log(e.detail.value.password);
    this.setData({
      Value: e.detail.value.password
    })
  },
  bindPhone() {
    if (this.data.Value.length < 6) {
      wx.showToast({
        title: '请填写手机验证码！',
        icon: "none"
      })
      return
    }

    service.getsmsJudge({
      "phone": this.data.phone,
      "result": this.data.Value,
      "userId": this.data.userId
    }, res => {
      //console.log(res)

      if (res.data == 1) {
        wx.showToast({
          title: '验证码输入错误！',
          icon: "none",
          duration: 2000
        })
      } else if (res.code == "B0001") {
        wx.showToast({
          title: '绑定失败！',
          icon: "none",
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '绑定成功！',
          duration: 2000
        })
        var storagedata = wx.getStorageSync('userInfo')
        storagedata.mobile = this.data.phone;
        wx.setStorage({
          key: "userInfo",
          data: storagedata,
          success: function () {
            wx.navigateBack()
          }
        });
      }
    })



  },
  getVerifityCode: function () {
    var that = this;
    if (this.data.phone == '') {
      this.setData({
        phoneErrorText: '电话号码不能为空！',
      });

      return
    }
    if (!rules.regPhone(this.data.phone)) {
      this.setData({
        phoneErrorText: '手机号格式有误！',
      });

      return
    }

    if (!that.data.counting) {

      service.getmessage(that.data.phone, res => {
        console.log(res)

        wx.showToast({
          title: '验证码已发送',
        })
        //开始倒计时
        countDown(that, that.data.second);
        that.setData({
          counting: true
        })
      })

    }

    function countDown(that, count) {
      if (count == 0) {
        that.setData({
          timeCountDownTop: '获取验证码',
          counting: false,
          second: 60
        })
        return;
      }

      that.setData({
        counting: true,
        timeCountDownTop: count + '秒后可重新获取',
        second: count
      })
      setTimeout(function () {
        count--;
        countDown(that, count);
      }, 1000);
    }
  },
  onLoad(options) {
    console.log(options)
    this.setData({
      phone: options.phone || '',
      userId: options.userId || wx.getStorageSync("userInfo").id
    })
  },
  goClause() {
    wx.navigateTo({
      url: '/pages/phone/clause/index',
    })
  },




})